package com.aliyun.oss.controller;

import com.aliyun.oss.pojo.DTO.AddFoodDTO;
import com.aliyun.oss.pojo.DTO.AddSetmealDish;
import com.aliyun.oss.pojo.DTO.EditCategoryDTO;
import com.aliyun.oss.pojo.DTO.SetMealDTO;
import com.aliyun.oss.pojo.Entity.Category;
import com.aliyun.oss.pojo.Entity.setMealDish;
import com.aliyun.oss.pojo.Vo.*;
import com.aliyun.oss.service.CategoryService;
import com.aliyun.oss.service.FoodService;
import com.aliyun.oss.service.SetMealDishService;
import com.aliyun.oss.service.SetMealService;
import com.aliyuncs.exceptions.ClientException;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/manage")
public class ManageController {
    @Autowired
    CategoryService categoryManageService;
    @Autowired
    FoodService foodService;
    @Autowired
    SetMealService setMealService;
    @Autowired
    SetMealDishService setMealDishService;
    @Autowired
    private RedisTemplate redisTemplate;


    @GetMapping("/getAllCategory")
    public ResultVO getAllCategory() {
        List<Category> categoryList = categoryManageService.getAllCategory();
        return ResultVO.success(categoryList);
    }

    @GetMapping("/getAllFoods")
    public ResultVO getAllFoods() {
        List<FoodVO> foodVOList = foodService.getAllFoods();
        return ResultVO.success(foodVOList);
    }

    @GetMapping("/getAllSetmeal")
    public ResultVO getAllSetmeal() {
        List<SetMealVO> setMealVOList = setMealService.getAllSetmeal();
        return ResultVO.success(setMealVOList);
    }

    //    查询category
    @GetMapping("/queryCategoryLikeByNameOrType")
    public ResultVO queryCategoryLikeByNameOrType(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "type", required = false) Integer type) {
        List<Category> categoryList = categoryManageService.queryCategoryLikeByNameOrType(name, type);
        return ResultVO.success(categoryList);
    }

    //    删除category
    @GetMapping("/deleteCategoryById")
    public ResultVO deleteCategoryById(String id) {
        categoryManageService.deleteCategoryById(id);
        return ResultVO.success();
    }

    @PatchMapping("/updateCategoryById")
    public ResultVO updateCategoryById(@RequestAttribute("id") Integer OperateId, @RequestBody EditCategoryDTO editCategoryDTO) {
        categoryManageService.updateCategoryById(OperateId, editCategoryDTO);
        return ResultVO.success();
    }

    @PatchMapping("/updateCategoryStatusById")
    public ResultVO updateCategoryStatusById(@RequestAttribute("id") Integer operateId, @RequestBody Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        Integer status = (Integer) map.get("status");
        categoryManageService.updateCategoryStatusById(operateId, id, status);
        return ResultVO.success();
    }

    //    添加分类
    @PostMapping("/addCategory")
    public ResultVO addCategory(@RequestAttribute("id") Integer operateId, @RequestBody EditCategoryDTO editCategoryDTO) {
        categoryManageService.addCategory(operateId, editCategoryDTO);
        return ResultVO.success();
    }

    //    查询菜品
    @GetMapping("queryFoodLikeByNameOrCategoryIdOrStatus")
    public ResultVO queryFoodLikeByNameOrCategoryIdOrStatus(String name, Integer categoryId, Integer status) {
        List<FoodVO> foodVOList = foodService.queryFoodLikeByNameOrCategoryIdOrStatus(name, categoryId, status);
        return ResultVO.success(foodVOList);
    }

    //    根据id删除菜品
    @DeleteMapping("deleteFoodById")
    public ResultVO deleteFoodById(Integer id) {
        foodService.deleteFoodById(id);
        return ResultVO.success();
    }

    //    根据id修改菜品状态
    @PatchMapping("/updateFoodStatusById")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public ResultVO updateFoodStatusById(@RequestAttribute("id") Integer operateId, @RequestBody Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        Integer status = (Integer) map.get("status");
        foodService.updateFoodStatusById(operateId, id, status);
//        为了方便，一旦使用批量删除，直接全部清理
//        cleanCache("dish_*");
        return ResultVO.success();
    }

    //    根据id批量删除菜品
    @DeleteMapping("/BatchDeleteFoodsByIds")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public ResultVO BatchDeleteFoodsByIds(String[] ids) {
        foodService.BatchDeleteFoodsByIds(ids);
//        System.out.println("!?!?"+ Arrays.toString(ids));
//        为了方便，一旦使用批量删除，直接全部清理
//        cleanCache("dish_*");
        return ResultVO.success();
    }

    //    添加菜品
    @PostMapping("/addFood")
    @CacheEvict(cacheNames = "setmealCache",key = "#addFoodDTO.categoryId")
    public ResultVO addFood(@RequestAttribute("id") Integer operateId, AddFoodDTO addFoodDTO) throws JsonProcessingException {
        foodService.addFood(operateId, addFoodDTO);
//        清除缓存数据
//        String key = "dish_" + addFoodDTO.getCategoryId();
//        cleanCache(key);
        return ResultVO.success();
    }

    //    根据id查询food
    @GetMapping("/getFoodById")
    public ResultVO getFoodById(@RequestAttribute("id") Integer operateId, Integer id) {
        GetFoodByIdVO getFoodByIdVO = foodService.getFoodById(operateId, id);
        return ResultVO.success(getFoodByIdVO);
    }

    //    更新菜品
    @PatchMapping("/updateFoodById")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public ResultVO updateFoodById(@RequestAttribute("id") Integer operateId, AddFoodDTO addFoodDTO) throws JsonProcessingException, ClientException {
        System.out.println("!?!?" + addFoodDTO.getImageUrl());
        foodService.updateFoodById(operateId, addFoodDTO);
//        为了方便，一旦使用批量删除，直接全部清理
//        cleanCache("dish_*");
        return ResultVO.success();
    }

    //    查询套餐
    @GetMapping("/querySetMealLikeByNameOrCategoryIdOrStatus")
    public ResultVO querySetMealLikeByNameOrCategoryIdOrStatus(String name, Integer categoryId, Integer status) {
        List<SetMealVO> setMealVOList = setMealService.querySetMealLikeByNameOrCategoryIdOrStatus(name, categoryId, status);
        return ResultVO.success(setMealVOList);

    }

    //    批量删除套餐
    @DeleteMapping("/BatchDeleteSetMealsByIds")
    public ResultVO BatchDeleteSetMealsByIds(String[] ids) {
        setMealService.BatchDeleteSetMealsByIds(ids);
        return ResultVO.success();
    }

    //    根据id删除套餐
    @DeleteMapping("/deleteSetMealById")
    public ResultVO deleteSetMealById(Integer id) {
        setMealService.removeById(id);
        return ResultVO.success();
    }

    //    根据id改变状态
    @PatchMapping("/updateSetMealStatusById")
    public ResultVO updateSetMealStatusById(@RequestAttribute("id") Integer operateId, @RequestBody Map<String, Object> map) {
        Integer id = (Integer) map.get("id");
        Integer status = (Integer) map.get("status");
        setMealService.updateSetMealStatusById(operateId, id, status);
        return ResultVO.success();
    }

    //    添加套餐
    @PostMapping("/addSetMeal")
    public ResultVO addSetMeal(@RequestAttribute("id") Integer operateId, SetMealDTO SetMealDTO) {
        setMealService.addSetMeal(operateId, SetMealDTO);
        return ResultVO.success();
    }

    //    根据id查询套餐
    @GetMapping("/getSetMealById")
    public ResultVO getSetMealById(Integer id) {
        FindByIdSetMealVO findByIdSetMealVO = setMealService.getSetMealById(id);
        return ResultVO.success(findByIdSetMealVO);
    }

    //    根据id更新套餐
    @PatchMapping("/updateSetMealById")
    public ResultVO updateSetMealById(@RequestAttribute("id") Integer operateId, SetMealDTO SetMealDTO) throws ClientException {
        System.out.println("123" + SetMealDTO);
        setMealService.updateSetMealById(operateId, SetMealDTO);
        return ResultVO.success();
    }

    //    添加套餐菜品
    @PostMapping("/addSetmealDish")
    public ResultVO addSetmealDish(@RequestBody List<AddSetmealDish> addSetmealDishes) {
        setMealDishService.addSetmealDish(addSetmealDishes);
        return ResultVO.success();
    }

//    清理redis方法
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
